package com.das.mechanic_base.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.QRUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.google.zxing.WriterException;
import com.kproduce.roundcorners.RoundTextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * 接车单详情-服务状态-转发-QR
 */
public class X3BottomForwardQRDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {

    private   String serviceUrl;
    ImageView iv_qr;
    RoundTextView rv_cancel , rv_affirm;

    Bitmap qrBitmap = null;

    public X3BottomForwardQRDialog(@NonNull Context context,String serviceUrl) {
        super(context);
        this.serviceUrl = serviceUrl;
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        rv_cancel = getView(R.id.rv_cancel);
        iv_qr = getView(R.id.iv_qr);
        rv_affirm = getView(R.id.rv_affirm);

        rv_cancel.setOnClickListener(this);
        rv_affirm.setOnClickListener(this);

        outputQR(serviceUrl);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_forward_qr_dialog;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rv_cancel){
            dismiss();
        } else if (v.getId() == R.id.rv_affirm){
            savePic2SystemAlbum(qrBitmap);
        }
    }

    private void outputQR(String url){
        Bitmap bitmapLogo = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.x3_ic_launcher);
        outputQR(url,bitmapLogo);
    }

    private void outputQR(String url,Bitmap bitmapLogo){
        try {
            qrBitmap = QRUtils.createCode(url, bitmapLogo);
            iv_qr.setImageBitmap(qrBitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    //保存二维码到系统相册
    private void savePic2SystemAlbum(Bitmap bitmap){
        String storePath;
        if (android.os.Build.VERSION.SDK_INT >= 29) {
            storePath = this.getContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + File.separator + this.getContext().getPackageName();
        } else {
            storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +  this.getContext().getPackageName();
        }
        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);

        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片
            boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
            fos.flush();
            fos.close();

            // 其次把文件插入到系统图库
            try {
                MediaStore.Images.Media.insertImage(this.getContext().getContentResolver(),
                        file.getAbsolutePath(), fileName, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 最后通知图库更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            this.getContext().sendBroadcast(intent);

            if (isSuccess) {
                X3ToastUtils.showMessage(this.getContext().getResources().getString(R.string.x3_car_detail_qr_save_suc));
            } else {
                X3ToastUtils.showMessage(this.getContext().getResources().getString(R.string.x3_car_detail_qr_save_fail));
            }
            Handler h = new Handler();
            h.postDelayed(new Runnable() {
                @Override
                public void run() {
                    dismiss();
                }
            },500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
